package cc.comac.mom.controller;

import java.io.File;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.QarRestrictedPartService;
import cc.comac.mom.api.lifelimit.LifeLimitPartBaseService;
import cc.comac.mom.bean.QarRestrictedPart;
import cc.comac.mom.bean.lifelimit.LiftLimitBaseParamBean;
import cc.comac.mom.bean.lifelimit.LimitResultBean;
import cc.comac.mom.entity.QarRestrictedPartEntity;
import cc.comac.mom.entity.print.RestrictedPartItemPrint;
import cc.comac.mom.util.BaseConnectionData;
import cc.comac.mom.util.JasperHelper;
import cc.comac.mom.util.ResultBean;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

@RestController
@RequestMapping(value = "/QarRestrictedPartController")
public class QarRestrictedPartController {

	@Autowired
	private QarRestrictedPartService qarRestrictedPartService;
	
	@Autowired
	private BaseConnectionData baseConnectionData;
	
	@Autowired
	private LifeLimitPartBaseService lifeLimitPartBaseService;
	// 限售件提交功能
	@RequestMapping(value = "/batchUpdateCheckData",method = RequestMethod.POST)
	public ResultBean updateCheckData(@RequestBody List<QarRestrictedPart> list) {
		ResultBean updateCheckData = qarRestrictedPartService.updateCheckData(list);
		return updateCheckData;
	}
	@RequestMapping(value = "/updateCheckData",method = RequestMethod.POST)
	public ResultBean batchInsertCheckGetData(@RequestBody QarRestrictedPartEntity list) {
		ResultBean batchInsertCheckGetData = qarRestrictedPartService.batchInsertCheckGetData(list);
		return batchInsertCheckGetData;
	}
	
	@RequestMapping(value = "/printRestrictedPartItem/{partNo}/{itemId}",method = RequestMethod.GET)
	public ResultBean printRestrictedPartItem(@PathVariable String partNo,@PathVariable long itemId,HttpServletRequest request,HttpServletResponse response) {
		ResultBean resultBean = new ResultBean();
		Map<String,String> map = new HashMap<String, String>();
		map.put("partNo", partNo);
		map.put("itemId", itemId+"");
		RestrictedPartItemPrint selectPrintData = qarRestrictedPartService.selectPrintData(map);
		try {
			 //指定模板文件
	        ClassPathResource classPathResource = new ClassPathResource("config/LifeLimitLabel.jasper");
			String exportFilePath="LifeLimitLabel";
			 File file=classPathResource.getFile();
		        Connection connection = baseConnectionData.getConnection();
			Map<String, Object> params = parseEntityToMap(selectPrintData);
			 JasperHelper.showPdf(exportFilePath, file.getPath(), request, response, params, connection);
			 
			 //			 JRDataSource jrDataSource = new JRBeanCollectionDataSource(null);
//			 JasperHelper.showPdf(exportFilePath, file.getPath(), request, response, params, jrDataSource);
		} catch (Exception e) {
			resultBean.setErrorMessge(e.getLocalizedMessage());
			e.printStackTrace();
		}
		return resultBean;
	}
	
	public Map<String,Object> parseEntityToMap(RestrictedPartItemPrint selectPrintData) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("Assembly_Number", selectPrintData.getAssembly_Number());
		map.put("Inspection", selectPrintData.getInspection());
		map.put("Limit_Serial_Number", selectPrintData.getLimit_Serial_Number());
		map.put("Limited_Method", selectPrintData.getLimited_Method());
		map.put("Limited_Time", selectPrintData.getLimited_Time());
		map.put("Part_No", selectPrintData.getPart_No());
		map.put("Reference_Date", selectPrintData.getReference_Date());
		map.put("Serial_Number", selectPrintData.getSerial_Number());
		map.put("Storage_Time", selectPrintData.getStorage_Time());
		return map;
	}
	
	// 判断当前件号是否为限售件
	@RequestMapping(value = "getLiftLimitBaseInfo/{partNo}",method = RequestMethod.GET)
	public ResultBean getLiftLimitBaseInfo (@PathVariable String partNo) {
		ResultBean resultBean = new ResultBean(true);
		LiftLimitBaseParamBean liftLimitBaseParamBean = new LiftLimitBaseParamBean();
		liftLimitBaseParamBean.setPartNo(partNo);
		LimitResultBean liftLimitBaseInfo = lifeLimitPartBaseService.getLiftLimitBaseInfo(liftLimitBaseParamBean);
		if (liftLimitBaseInfo.getCode() != 1) {
			resultBean.setErrorMessge(liftLimitBaseInfo.getMsg());
		}
		return resultBean;
	}
}
